package cn.shadow.game.chess.CHnchess.chessman;


import cn.shadow.game.chess.CHnchess.board.Board;
import cn.shadow.game.chess.CHnchess.board.Line;
import cn.shadow.game.chess.CHnchess.board.Position;
import cn.shadow.game.chess.CHnchess.type.Camp;
import cn.shadow.game.chess.CHnchess.type.ChessType;

/**
 * 车
 */
public class Che extends AbstractChess implements Line {

    public Che(Board board, Camp camp) {
        super(ChessType.CHE, board, camp);
    }

    public void addLeftPoint() {
        int row = getRow();
        int col = getCol();
        for (int j = col - 1; j >= 0; j--) {
            Position each = getBoard().getPoint(row, j); //直线上每一个点
            if (addPoint(each)) {
                break;
            }
        }
    }

    public void addRightPoint() {
        int row = this.getRow();
        int col = this.getCol();
        for (int j = col + 1; j < Board.COL; j++) {
            Position each = getBoard().getPoint(row, j); //直线上每一个点
            if (addPoint(each)) {
                break;
            }
        }
    }

    public void addUpPoint() {
        int row = getRow();
        int col = getCol();
        for (int i = row - 1; i >= 0; i--) {
            Position each = getBoard().getPoint(i, col); //直线上每一个点
            if (addPoint(each)) {
                break;
            }
        }
    }

    public void addDownPoint() {
        int row = getRow();
        int col = getCol();
        for (int i = row + 1; i < Board.ROW; i++) {
            Position each = getBoard().getPoint(i, col); //直线上每一个点
            if (addPoint(each)) {
                break;
            }
        }
    }


    private boolean addPoint(Position each) {
        Chess chess = each.getChess();
        if (chess != null) { //找到一个点
            if (chess.getCamp() == this.getCamp()) { //己方
                return true;
            } else { //敌方
                canSetPosition.add(each);
                canAttackPosition.add(each);
                return true;
            }
        }
        canSetPosition.add(each);
        canAttackPosition.add(each);
        return false;
    }
}
